package demo;

import java.util.Arrays;
import java.util.List;

/**
 * 环境检查器主类
 * 负责执行整个环境检查流程
 */
public class EnvironmentChecker {

    /**
     * 执行环境检查
     * @param logs 日志列表
     * @return 构建好的环境检查树根节点
     */
    public static TreeNode checkEnvironment(List<LogEntry> logs) {
        // 1. 构建初始树结构
        TreeNode root = EnvironmentCheckTreeBuilder.buildTree();

        // 2. 根据日志更新各节点状态和使能
        EnvironmentCheckTreeBuilder.updateNodeStatusAndEnable(logs);

        return root;
    }

    /**
     * 主方法，用于测试环境检查功能
     * @param args 命令行参数
     */
    // 测试用例
    public static void main(String[] args) {
        List<LogEntry> logs = Arrays.asList(
            new LogEntry("log_enable_1", "enable", "true"),
            new LogEntry("log_status_1", "status", "成功"),
            new LogEntry("log_enable_2", "enable", "false"),
            new LogEntry("log_status_2", "status", "失败"),
            new LogEntry("log_enable_3", "enable", "true"),
            new LogEntry("log_status_3", "status", "成功"),
            new LogEntry("log_enable_4", "enable", "true"),
            new LogEntry("log_status_4", "status", "成功"),
            new LogEntry("log_enable_5", "enable", "true"),
            new LogEntry("log_status_5", "status", "失败")
        );

        TreeNode result = checkEnvironment(logs);

        // 输出结果（简化打印）
        printTree(result, 0);
    }

    /**
     * 递归打印树结构
     * @param node 当前节点
     * @param level 当前层级
     */
    private static void printTree(TreeNode node, int level) {
        String indent = "  ".repeat(level);
        System.out.println(indent + node.name + " [使能=" + node.enabled + ", 状态=" + node.status + "]");
        for (TreeNode child : node.children) {
            printTree(child, level + 1);
        }
    }
}